<template>
  <div class="box">
    <div class="topboxone">
      <el-button type="primary" @click="addbutton">新增</el-button>
      <el-button type="primary" @click="xgbutton"> 修改 </el-button>
      <el-button type="primary" @click="deletebutton"> 删除 </el-button>
    </div>
    <div>
      <el-table :data="datalist" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="recordTime" label="记录时间" />
        <el-table-column prop="questionnaireType" label="问卷类型" />
        <el-table-column prop="score" label="问卷评分" />
        <!-- <el-table-column prop="photoUrl" label="文件" /> -->
        <el-table-column prop="remark" label="备注" />
      </el-table>
      <div class="fybox">
        <el-pagination
          v-model:current-page="form.pageNum"
          v-model:page-size="form.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="total, sizes, prev, pager"
          :total="pagetotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="新增问卷" width="600" :before-close="handleClose">
      <el-form :model="form" label-width="auto" style="max-width: 600px" ref="ruleFormRef">
        <el-form-item label="上传问卷 ">
          <el-upload v-model:file-list="fileListzy" class="upload-demo" multiple :limit="1" :http-request="uploadFile">
            <el-button type="primary">上传问卷</el-button>
            <template #tip>
              <div class="el-upload__tip"></div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-date-picker v-model="form.recordTime" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>

        <el-form-item label="问卷类型" prop="questionnaireType">
          <el-input v-model="form.questionnaireType" />
        </el-form-item>

        <el-form-item label="得分" prop="score">
          <el-input v-model="form.score" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" />
        </el-form-item>

        <el-form-item>
          <div class="savrnutton">
            <el-button type="primary" @click="onSubmit(ruleFormRef)">保存</el-button>
            <el-button @click="clearbutton(ruleFormRef)">清空</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { questionnaire, uploadAPI, getInfowj, ejgetInfo, questionnairexg, questionnairedel } from "@/api/hzmange/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute } from "vue-router";
const fileListzy = ref([]);
const parentlist = JSON.parse(sessionStorage.getItem("patientInfo"));
let datalist = ref([]);
let dialogVisible = ref(false);
const ruleFormRef = ref(null);
const size = ref("default");
var pagetotal = ref(0);
const form = ref({});
let selection = ref([]);
const handleSelectionChange = e => {
  selection.value = e;
};
const onSubmit = formEl => {
  form.value.patientId = parentlist.id; //患者ID
  if (addtype.value) {
    //新增呢、
    questionnaire(form.value).then(res => {
      ElMessage.success("新增成功");
      getList();
    });
  } else {
    questionnairexg(form.value).then(res => {
      ElMessage.success("修改成功");
      getList();
    });
  }
  dialogVisible.value = false;
};
const clearbutton = formEl => {
  if (!formEl) return;
  formEl.resetFields();
};
const handleClose = () => {
  dialogVisible.value = false;
};
let addbutton = () => {
  addtype.value = true;
  form.value = {};
  dialogVisible.value = true;
};
const seebutton = e => {
  console.log(e);
  dialogVisible.value = true;
};
let uploadFile = val => {
  const fd = new FormData();
  fd.append("file", val.file);
  uploadAPI(fd).then(res => {
    if (res.code === 200) {
      ElMessage({
        message: "上传成功",
        type: "success"
      });
      form.value.photoUrl = res.url;
    } else {
      ElMessage({
        message: res.msg,
        type: "error"
      });
    }
  });
};
var addtype = ref(true);
function xgbutton() {
  addtype.value = false;
  if (selection.value.length == 1) {
    ejgetInfo({
      id: selection.value[0].id
    }).then(res => {
      dialogVisible.value = true;
      form.value = res.data;
      fileListzy.value = []; //文件回显
      let feilname = res.data.reportUrl.substring(res.data.reportUrl.lastIndexOf("/") + 1);
      fileListzy.value.push({ name: feilname, url: res.data.reportUrl }); //文件回显
    });
  } else {
    ElMessage({
      message: "请选择一条数据",
      type: "warning"
    });
  }
}
let handleSizeChange = () => {
  getList();
};
let handleCurrentChange = () => {
  getList();
};
// 初始化数据
getList();
// 获取数据
function getList() {
  getInfowj({ patientId: parentlist.id }).then(res => {
    datalist.value = res.data;
    pagetotal.value = res.total;
  });
}
//删除
const deletebutton = e => {
  if (selection.value.length == 1) {
    ElMessageBox.confirm("确定删除?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      questionnairedel({
        id: selection.value[0].id
      }).then(res => {
        ElMessage({
          type: "success",
          message: `${res.msg}`
        });
        getList();
      });
    });
  } else {
    ElMessage({
      message: "请选择一条数据",
      type: "warning"
    });
  }
};
</script>
<style scoped>
.box {
  color: #606266;
}
.topbox {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.savrnutton {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
.topboxone {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
</style>
